package com.syty.demo.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syty.demo.domain.User;
import com.syty.demo.mapper.UserMapper;
import com.syty.demo.service.UserService;
import com.syty.demo.util.ApplicationContextUtil;
import com.syty.demo.util.LogUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.SimpleRole;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.Collection;


@Api("Shiro-自定义UserRealm")
public class UserRealm extends AuthorizingRealm {

    @ApiOperation("授权管理")
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        LogUtils.write("[SHIRO授权]--开始授权");
        //获取serviceImpl信息
        UserService userService = (UserService) ApplicationContextUtil.getBean("userServiceImpl");
        //获取身份信息
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
        //根据主身份信息获取权限身份信息
        //获取用户信息
        String role = userService.findRoleByUsername(primaryPrincipal);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //判断用户角色
        if ("root".equals(role)){
            simpleAuthorizationInfo.addRole("admin");
            LogUtils.write("[SHIRO授权]--当前用户权限 : admin");
            LogUtils.write("[SHIRO授权]--结束授权");
            return simpleAuthorizationInfo;
        }else {
            simpleAuthorizationInfo.addRole("user");
            LogUtils.write("[SHIRO授权]--当前用户权限 : user");
            LogUtils.write("[SHIRO授权]--结束授权");
            return simpleAuthorizationInfo;
        }
    }

    @ApiOperation("认证管理")
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        LogUtils.write("[SHIRO认证]--开始认证");

        UserService userService = (UserService) ApplicationContextUtil.getBean("userServiceImpl");
        LogUtils.write("[SHIRO认证]--获取userService-bean ： "+userService);

        String principal = (String) token.getPrincipal();
        LogUtils.write("[SHIRO认证]--获取认证信息 ： "+principal);

        //      获取认证信息中的用户名和密码
        String username = (String)token.getPrincipal();
        String password = new String((char[])token.getCredentials());
        LogUtils.write("[SHIRO认证]--获取凭证中用户信息 ： "+username);
        LogUtils.write("[SHIRO认证]--获取凭证中用户密码 ： "+password);

        //      通过认证信息获取数据库中的用户信息
        User user = userService.findUserByUsername(principal);

        //检查数据库中的用户信息是否为空
        if (!ObjectUtils.isEmpty(user)){
            LogUtils.write("[SHIRO认证]--获取数据库用户信息 ： "+user);
                //如果未登录，进行放行，完成认证
                LogUtils.write("[SHIRO认证]--认证完成");
                //如果不为空
                return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes(user.getSalt()), this.getName());

        }
        LogUtils.write("[SHIRO认证]--认证失败");

        return null;
    }
}
